import 'package:flutter/material.dart';
import 'package:xvideos/assets_tool/app_colors.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

typedef RefreshViewOnInit = Function(RefreshController controller);

class CustomRefreshView extends StatefulWidget {
  final bool enablePullDown;
  final bool enablePullUp;
  final Function(RefreshController? ctr)? onRefresh;
  final Function(RefreshController? ctr)? onLoading;
  final Widget? customHeader;
  final Widget? footerHeader;
  final Widget child;
  final String noDataText;
  final RefreshViewOnInit onInit;

  const CustomRefreshView({
    super.key,
    required this.child,
    required this.onInit,
    this.enablePullDown = true,
    this.enablePullUp = true,
    this.onRefresh,
    this.onLoading,
    this.customHeader,
    this.footerHeader,
    this.noDataText = "——  没有更多数据了  ——",
  });

  @override
  State<CustomRefreshView> createState() => _CustomIRefreshViewState();
}

class _CustomIRefreshViewState extends State<CustomRefreshView> {
  final refreshController = RefreshController();

  @override
  void initState() {
    widget.onInit(refreshController);
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return SmartRefresher(
        enablePullDown: widget.enablePullDown,
        enablePullUp: widget.enablePullUp,
        controller: refreshController,
        onRefresh: () => widget.onRefresh?.call(refreshController),
        onLoading: () => widget.onLoading?.call(refreshController),
        header: widget.customHeader ??
            const WaterDropHeader(
              waterDropColor: AppColors.tipTextColor99,
              complete: Text(
                "刷新完成！",
                style: TextStyle(color: AppColors.tipTextColor99, fontSize: 12),
              ),
              failed: Text(
                "刷新失敗!",
                style: TextStyle(color: AppColors.tipTextColor99, fontSize: 12),
              ),
            ),
        footer: widget.footerHeader ??
            ClassicFooter(
              loadingText: "加载中...",
              canLoadingText: "松开加载更多...",
              noDataText: widget.noDataText,
              idleText: "上拉加载更多",
              textStyle: TextStyle(
                color: AppColors.tipTextColor99,
                fontSize: 12,
                decoration: TextDecoration.none,
                fontWeight: FontWeight.normal,
              ),
            ),
        child: widget.child);
  }
}

Widget pullYsRefresh({
  required RefreshViewOnInit onInit,
  required Widget child,
  Key? key,
  final Function(RefreshController? ctr)? onRefresh,
  final Function(RefreshController? ctr)? onLoading,
  bool enablePullUp = true,
  bool enablePullDown = true,
  String noDataText = "——  没有更多数据了  ——",
  Widget? customHeader,
  Widget? footerHeader,
}) {
  return CustomRefreshView(
    child: child,
    onInit: onInit,
    onLoading: onLoading,
    onRefresh: onRefresh,
    customHeader: customHeader,
    footerHeader: footerHeader,
    enablePullDown: enablePullDown,
    enablePullUp: enablePullUp,
    noDataText: noDataText,
  );
}
